package com.aldobucchi.hierarchy
{
	

	import com.aldobucchi.hierarchy.rel.HierarchicalRelation;
	import com.aldobucchi.hierarchy.rel.Higher;
	import com.aldobucchi.hierarchy.rel.Lower;



	public class HierarchyUtil
	{
		
		
		

		/**
		 * 
		 * 
		 * 
		 * @param concepts
		 * @param removeHigher
		 * 		If set to true, removes concepts that are "higher" than others
		 * 		If set to false, removes concepts that are "lower" than others.
		 * @return 
		 * 
		 */		
		public static function optimize( concepts:Array, removeHigher:Boolean = true ):Array
		{
			
			var i:int, j:int, l:int = concepts.length;
			
			var marked:Array = [ ];
			
			
			for ( i=0; i<l; i++ )
			{
				for ( j=0; j<l; j++ )
				{
					if ( i == j ) // do not compare with self
						continue;
					
					var t1:IHierarchicalConcept = concepts[ i ];
					var t2:IHierarchicalConcept = concepts[ j ];
					
					var tr:HierarchicalRelation = t1.getHierarchicalRelation( t2 );
					
					if ( removeHigher && ( tr is Higher ) )
						marked[ i ] = true;
					else if ( !removeHigher && ( tr is Lower ) )
						marked[ i ] = true;
				}
			}
		
			
			var result:Array = [ ];
			
			for ( i=0; i<l; i++ )
				if ( ! marked[ i ] )
					result.push( concepts[ i ] );
		
			return result;
		
		}
				
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
				

	}

}